package org.deng.java.JUC;


/**
 * packageName org.deng.java.JUC
 *
 * @author dengjinzhang
 * @version JDK 1.8
 * @date 2024/9/29
 * @description TODO
 */
public class SaleTicketDemoSynchronize {
    public static void main(String[] args) {
        // 资源对象ticket
        Ticket1 ticket = new Ticket1();
        //new三个线程来模拟多线程环境下的锁相互竞争
        new Thread(()->{for (int i=0 ;i<70;i++){ticket.sale();}},"A").start();
        new Thread(()->{for (int i=0 ;i<70;i++){ticket.sale();}},"B").start();
        new Thread(()->{for (int i=0 ;i<70;i++){ticket.sale();}},"C").start();
    }
}
//票类，包含属性票数和买票的方法sale() 模拟多线程的的资源。
class  Ticket{
    //票数
    private int number = 60;

    //售票方法
    public synchronized void sale(){
        if(number>0){System.out.println(Thread.currentThread().getName()+"卖出了第："+number--+"张票，剩余："+number);}
    }
}
